import axios from 'axios';
import { Form } from 'element-ui';

// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};
// 通用公用方法
const req = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            Authorization: localStorage.getItem('logintoken')
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};

//通用上传文件方法
const upload = (method,url,files) =>{
    
    console.log(files);
    var formData = new FormData()
    files.forEach(file => {
        formData.append('file', file);
    });
     // 将文件添加到FormData对象中
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-type': 'multipart/form-data',
            Authorization: localStorage.getItem('logintoken')
        },
        data: formData
    }).then(res => res.data);
}



export {
    loginreq,
    req,
    upload
}